﻿using System;
using System.Linq;
using Coevery.ViewModel.EntityDataService;
using System.Data.Services.Client;
using System.Windows.Controls.Primitives;

namespace Coevery.ViewModel
{
    public class LeadViewModel:BaseViewModel
    {
        public Guid LeadId { get; set; }
        public Popup Popup { get; set; }
        public event EventHandler EnableButton;

        private DataServiceCollection<Lead> _leads;

        public DataServiceCollection<Lead> Leads
        {
            get { return _leads; }
            set
            {
                _leads = value;
                _leads.LoadCompleted += LoadLeadData;
                OnPropertyChanged("Leads");
            }
        }

        private DataServiceCollection<Note> _notes;

        public DataServiceCollection<Note> Notes
        {
            get { return _notes; }
            set
            {
                _notes = value;
                _notes.LoadCompleted += LoadCommentData;
                OnPropertyChanged("Notes");
            }
        }

        public void LoadData()
        {
            if (!Popup.IsOpen)
            {
                Popup.IsOpen = true;
                EnableButton.Invoke(false, null);
            }
           
            Leads = new DataServiceCollection<Lead>(Context);
            Notes = new DataServiceCollection<Note>(Context);
            var query = (from cust in Context.Leads
                         where cust.LeadId == LeadId && cust.IsDeleted == false
                         select new Lead
                         {
                             LeadId = cust.LeadId,
                             FullName = cust.FullName,
                             Subject = cust.Subject,
                             Country = cust.Country,
                             ModifiedOn = cust.ModifiedOn,
                             MobilePhone = cust.MobilePhone,
                             EmailAddress = cust.EmailAddress,
                             WebSiteUrl = cust.WebSiteUrl,
                             Description = cust.Description,
                             StatusCode = cust.StatusCode,
                             Address = cust.Address,
                             City = cust.City,
                             CompanyName = cust.CompanyName,
                             CountryCode = cust.CountryCode,
                             CreatedById = cust.CreatedById,
                             CustomerId = cust.CustomerId,
                             DepartmentId = cust.DepartmentId,
                             Fax = cust.Fax,
                             FirstName = cust.FirstName,
                             IsDeleted = cust.IsDeleted,
                             LastName = cust.LastName,
                             LeadSourceCode = cust.LeadSourceCode,
                             MiddleName = cust.MiddleName,
                             ModifiedById = cust.ModifiedById,
                             NumberOfEmployees = cust.NumberOfEmployees,
                             OriginalMessage = cust.OriginalMessage,
                             OwnerId = cust.OwnerId,
                             PriorityCode = cust.PriorityCode,
                             Revenue = cust.Revenue,
                             Technology = cust.Technology,
                             TechnologyCode = cust.TechnologyCode,
                             Telephone1 = cust.Telephone1,
                             Telephone2 = cust.Telephone2,
                             VersionNumber = cust.VersionNumber,
                             ZIP = cust.ZIP
                         });
            Leads.LoadAsync(query);
          
        }

        public void LoadLeadData(object sender, LoadCompletedEventArgs e)
        {
            if (Leads.Continuation != null)
            {
                Leads.LoadNextPartialSetAsync();
            }
            Notes.LoadAsync((from cust in Context.Notes.Expand("ModifiedBy") where cust.ObjectType == "Lead" && cust.ObjectId == LeadId && cust.IsDeleted == false orderby cust.CreatedOn descending select cust));
        }

        public void LoadCommentData(object sender, LoadCompletedEventArgs e)
        {
            if (Notes.Continuation != null)
            {
                Notes.LoadNextPartialSetAsync();
            }

            Popup.IsOpen = false;
            EnableButton.Invoke(true, null);
        }
    }
}